package atom.core0.bean.general;

import atom.core0.util.RandomUtil;


//2位报文flag+28位ID+8位长度+内容
//8位长   99999999=95M
public class MyProtocalPack {     
    private String flag="  ";//2位长 为了将来用途，目前只是打印输出     
    private String id="";//28长度
    
    private byte[] msgBody;     
    public static final int PackHeadLength=38;   
    public static final int MaxPackLength = 10*1024*1024; //定义最大10M
    public MyProtocalPack(){     
             
    }     
         
    public MyProtocalPack(String flag,byte[] msgBody){     
    	this.id = RandomUtil.genRandomUUID();
        this.flag=flag;     
        this.msgBody=msgBody;     
    }     
    public MyProtocalPack(String flag,String id,byte[] msgBody){     
        this.flag=flag;     
        this.id = id.trim();
        this.msgBody=msgBody;     
    }           

    public String getId()
    {
    	return id;
    }
	public byte[] getMsgHead()
    {
		byte[] msgHead=new byte[38];  
		
		System.arraycopy(getFlag().getBytes(), 0, msgHead, 0, 2);
		String newid;
		if (id.length()>28)
			newid = id.substring(id.length()-28);
		else
			newid = String.format("%28s",id);
		
		System.arraycopy(newid.getBytes(), 0, msgHead, 2, 28);
		
		System.arraycopy(getLengthString().getBytes(), 0, msgHead, 30, 8);
		
    	return msgHead;
    }

	public byte[] getMsgBody()
    {
    	return msgBody;
    }

	public void setMsgBody(byte[] msgBody)
    {
    	this.msgBody = msgBody;
    }

	public void setId(String id)
    {
    	this.id = id;
    }

	public String getFlag()
    {
    	String newFlag;
   		newFlag = String.format("%2s",flag).substring(0,2);
    	return newFlag;
    }

	public void setFlag(String flag)
    {
    	this.flag = flag;
		if (flag==null)
			this.flag="";
		
    }



   
	public String getLengthString() {     
		String slen = String.format("%08d", msgBody.length);
        return slen;     
    }     
 

         
    public String toString(){     
        StringBuffer sb=new StringBuffer();     
        sb.append("Flag=").append(flag);     
        sb.append(",Id=").append(id);     
        sb.append(",Len=").append(msgBody.length);     
        return sb.toString();     
    }     
}    
